<template>
  <div class="call-window">
    <!-- 自定义标题栏 -->
    <div class="title-bar">
      <span class="title">通话中</span>
      <div class="window-btn-container">
        <button @click="minimizeWindow" class="window-btn">—</button>
        <button @click="toggleMaximizeWindow" class="window-btn">{{ isMaximized ? '🗖' : '〇' }}</button>
        <button @click="closeWindow" class="window-btn">X</button>
      </div>
    </div>

    <!-- 视频/语音通话显示区域 -->
    <div class="video-container" v-if="isVideo">
      <!-- 右上角的小视频 -->
      <video ref="remoteVideo" autoplay playsinline :class="isRemoteLarge ? 'top-right' : 'center'"></video>
      <!-- 中心的大视频 -->
      <video ref="localVideo" autoplay playsinline muted :class="!isRemoteLarge ? 'top-right' : 'center'"></video>
    </div>
    <div class="audio-container" v-else>
      <p>语音通话中...</p>
    </div>

    <!-- 控制按钮部分 -->
    <div class="controls">
      <div class="control-group">
        <button @click="toggleMic" class="control-btn mic-btn">
          <img :src="micOn ? micOnIcon : micOffIcon" alt="Microphone" />
        </button>
        <span class="control-text">{{ micOn ? '麦克风开启' : '麦克风关闭' }}</span>
      </div>

      <div class="control-group">
        <button @click="hangUp" class="control-btn hangup-btn">
          <img :src="hangupIcon" alt="Hangup" />
        </button>
        <span class="control-text">挂断</span>
      </div>

      <div class="control-group">
        <button @click="toggleCamera" class="control-btn camera-btn">
          <img :src="cameraOn ? cameraOnIcon : cameraOffIcon" alt="Camera" />
        </button>
        <span class="control-text">{{ cameraOn ? '摄像头开启' : '摄像头关闭' }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import webrtcClient from '../../utils/webrtcClient.js';

// 引入下方三个按钮所需的 PNG 图标
import micOnIcon from '../../assets/icons/Svg/micOnIcon.png';
import micOffIcon from '../../assets/icons/Svg/micOffIcon.png';
import hangupIcon from '../../assets/icons/Svg/hangupIcon.png';
import cameraOnIcon from '../../assets/icons/Svg/cameraOnIcon.png';
import cameraOffIcon from '../../assets/icons/Svg/cameraOffIcon.png';

const isVideo = ref(true);
const micOn = ref(true);
const cameraOn = ref(true); // 改为控制摄像头的状态
const isMaximized = ref(false);
const isRemoteLarge = ref(true);

const remoteAvatar = ref('https://path-to-avatar.jpg'); // 更新为对方头像的 URL

const toggleMaximizeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('toggle-maximize');
  isMaximized.value = !isMaximized.value;
};

const minimizeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('minimize-window');
};

const closeWindow = () => {
  window.electronAPI?.ipcRenderer?.send('close-window');
};

const toggleMic = () => {
  micOn.value = !micOn.value;
  webrtcClient.toggleAudio(micOn.value);
};

// 修改为控制摄像头开关
const toggleCamera = () => {
  cameraOn.value = !cameraOn.value;
  webrtcClient.toggleVideo(cameraOn.value); // 调用 webrtcClient 中的 toggleVideo 方法
};

const hangUp = () => {
  webrtcClient.closeConnection();
  closeWindow();
};

defineExpose({ hangUp });
</script>

<style scoped>
.call-window {
  position: fixed;
  width: 100vw;
  height: 100vh;
  background: #222;
  color: #fff;
  z-index: 1000;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

.title-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: #333;
  border-radius: 10px 10px 0 0;
  color: #fff;
}

.title {
  font-size: 18px;
  flex-grow: 1;
}

.window-btn-container {
  display: flex;
  justify-content: flex-end;
}

.window-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  margin-left: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
}

.video-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.avatar-container {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 2px solid #fff;
}

.audio-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  background: #333;
  border-radius: 0 0 10px 10px;
}

.control-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0 15px;
}

.control-btn {
  background-color: #444;
  color: #fff;
  border: none;
  padding: 15px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60px;
  height: 60px;
  margin-bottom: 5px;
}

.control-btn:hover {
  background-color: #555;
}

.control-text {
  font-size: 10px;
  text-align: center;
}

.hangup-btn {
  background-color: red;
}

.hangup-btn:hover {
  background-color: darkred;
}

video.top-right {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 120px;
  height: 160px;
}

video.center {
  position: relative;
  width: 80%;
  height: auto;
}

/* 调整 PNG 图片样式 */
img {
  width: 24px;
  height: 24px;
}
</style>
